<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">注册</h3>
      <el-form-item prop="loginName">
        <el-input
          v-model="registerForm.loginName"
          type="text"
          auto-complete="off"
          placeholder="用户名"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="email">
        <el-input v-model="registerForm.email" type="text" auto-complete="off" placeholder="邮箱账号">
          <svg-icon slot="prefix" icon-class="message" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="phone">
        <el-input
          v-model="registerForm.phone"
          type="phone"
          auto-complete="off"
          placeholder="手机号"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="emailCode" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.emailCode"
          auto-complete="off"
          placeholder="验证码"
          style="width: 58%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <el-button type="primary" size="smail" style="float:right" @click="getEmailCode">{{ verifyCode }}</el-button>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { setEmailCode, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    return {
      codeUrl: "",
      verifyCode:"发送邮箱验证码",
      totalTime: 60,
      registerForm: {
        loginName: "",
        password: "",
        email:"",
        emailCode: "",
        uuid: ""
      },
      registerRules: {
        loginName: [
          { required: true, trigger: "blur", message: "请输入您的用户名" },
          { min: 2, max: 20, message: '邮箱账号长度在 20 之内', trigger: 'blur' }
        ],
        email: [
          { required: true, trigger: "blur", message: "请输入您的邮箱账号" },
          { min: 2, max: 20, message: '邮箱账号长度在 20 之内', trigger: 'blur' }
        ],
        phone: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
          { min: 11, max: 11, message: '手机号必须为11位数', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        emailCode: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    
  },
  methods: {
    //获取验证码
    getEmailCode(){
        //判断邮箱账号是否为空
        if(!this.registerForm.loginName){
            this.$message.error("请输入用户名！")
            return
        }
        if(!this.registerForm.email){
            this.$message.error("请输入邮箱账号！")
            return
        }
        //判断用户输入的邮箱格式是否正确
        if(!/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+/.test(this.registerForm.email)){
            this.$message.error("请输入正确的邮箱账号！")
            return
        }
        if(!/^1[3-9]\d{9}$/.test(this.registerForm.phone)){
          this.$message.error("请输入正确的手机号！")
        }
        //发送邮箱账号获取验证码
        setEmailCode(this.registerForm).then(res =>{
            //请求发送成功 开始倒计时
            this.verifyCode = this.totalTime + "s后重新发送"
            let clock = window.setInterval(() => {
            //倒计时功能
            this.totalTime--
            this.verifyCode = this.totalTime + 's后重新发送'
            if (this.totalTime < 0) {
                window.clearInterval(clock)
                this.verifyCode = '重新获取验证码'
                this.totalTime = 60
                this.canClick = false
                }
            }, 1000)
            this.$message.success("发送成功");
        })
        .catch(error =>{
            this.$message.error(error);
        })
        },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const loginName = this.registerForm.loginName;
            this.$alert("<font color='red'>恭喜你，您的账号 " + loginName + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
